<!DOCTYPE html>
<html lang="zh-cn">
<head>
	<{wapheader}>
	<link rel="stylesheet" href="images/qingfeng.css">
	<link rel="stylesheet" href="images/index.css">
	<link rel="stylesheet" href="images/swiper.min.css">
    <link rel="stylesheet" href="images/css/ynyq.css">
	<!--<script src="images/qingfeng.js"></script>-->
</head>
<body class="index-page">

<{wap_require file="block/footer.html"}>
    <div class="g-content">
        <header class="bg-red">
            <a  class="u-left u-scanQR"><img src="/wap_themes/xiaomi/images/icon/QR-icon.png" alt=""></a>
            <p class="u-title">江西喜洋洋</p>
            <a href="<{link app=b2c ctl=wap_passport act=login}>" class="u-right u-login"><span>登录</span></a>
            <a href="<{link app=b2c ctl=wap_passport act=logout}>" class="u-right u-logout" style="display: none"><span>退出登录</span></a>
        </header>
        <div class="slider"><{wap_widgets id="w4"}></div>
        <div class="header" id="header">
            <div class="searchbar">
                <{wap_widgets id="w2"}>
                <a class="u-left u-scanQR"><img src="/wap_themes/xiaomi/images/icon/QR-icon.png" alt=""></a>
            </div>
        </div>
        <div class="broadcast-list">
            <{wap_widgets id="btn"}>
        </div>
        <div style="display:none"class="broadcast-list">
            <{wap_widgets id="w18"}>
        </div>
        <div class="index-fore-adv">
            <div class="w">
                <{wap_widgets id="wadv"}>
            </div>
        </div>
        <div class="index-fore3">
            <div class="w">
                <{wap_widgets id="w5"}>
            </div>
        </div>

        <div class="index-fore4">
            <div class="w">
                <{wap_widgets id="w8"}>
            </div>
        </div>
        <div class="index-fore5">
            <div class="w">
                <{wap_widgets id="w6"}>
            </div>
        </div>
        <div class="index-fore6">
            <div class="w">
                <{wap_widgets id="w12"}>
            </div>
        </div>
        <div class="index-fore7">
            <div class="w">
                <{wap_widgets id="w16"}>
            </div>
        </div>

        <div style="display:none" class="index-fore8">
            <div class="w">
                <{wap_widgets id="w13"}>
            </div>
        </div>

        <div style="display:none" class="index-fore9">
            <div class="w">
                <{wap_widgets id="w14"}>
            </div>
        </div>
        <div class="index-fore10">
            <div class="w">
                <{wap_widgets id="w15"}>
            </div>
        </div>
        <div style="display:none" class="index-fore11">
            <div class="w">
                <{wap_widgets id="w9"}>
            </div>
        </div>
        <div style="display:none" class="index-fore12">
            <div class="w">
                <{wap_widgets id="w10"}>
            </div>
        </div>
        <div style="display:none" class="index-fore13">
            <div class="w">
                <{wap_widgets id="w11"}>
            </div>
        </div>
        <div style="display:none" class="index-fore14">
            <div class="w">
                <{wap_widgets id="w17"}>
            </div>
        </div>
        <div style="display:none" class="index-fore15">
            <div class="w">
                <{wap_widgets id="w7"}>
            </div>
        </div>

    </div>
<{wapfooter}>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>


    var e = $.fn.cookie('UNAME')?$.fn.cookie('UNAME'):'';
    console.log(e);
    if(e){
        $(".u-login").hide();
        $(".u-logout").show();
    }


    $(function () {
        //初始化session

        sessionInit_index();

        //获取商品水印个数

        $.get("<{link app=b2c ctl=wap_cart act=getcart_data }>",function(re){
            re=JSON.parse(re);
            for(i in re){
                if($(".pt-item[data-id='"+re[i].obj_ident+"']").length>0){
                    var $this=$(".pt-item[data-id='"+re[i].obj_ident+"']").find(".pt-img");
                    var num= parseInt(re[i].quantity);
                    var box=$("<p>").addClass("u-cart-num").html(num);
                    $this.append(box);
                }
            }

        });


        var _appid="<{$SignPackage.appId}>";
        var _timestamp="<{$SignPackage.timestamp}>";
        var _nonceStr="<{$SignPackage.nonceStr}>";
        var _signature="<{$SignPackage.signature}>";

        $(document).on("scroll",function () {
            sessionStorage.scrollTop_index=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
        });
        if(sessionStorage.scrollTop_index!=0){
            document.body.scrollTop=sessionStorage.scrollTop_index;
            document.documentElement.scrollTop=sessionStorage.scrollTop_index;
        }

        wx.config({
            // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            debug: false,
            // 必填，公众号的唯一标识result.appId
            appId: _appid,
            // 必填，生成签名的时间戳result.timestamp
            timestamp: _timestamp,
            // 必填，生成签名的随机串
            nonceStr: _nonceStr,
            // 必填，签名，见附录1
            signature: _signature,
            // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            jsApiList : [ 'checkJsApi', 'scanQRCode' ]
        });
        wx.error(function(re) {
//            alert("出错了：" + re.errMsg);
        });
        wx.ready(function() {
            wx.checkJsApi({
                jsApiList : ['scanQRCode'],
                success : function(res) {

                }
            });
        });


        $(".u-scanQR").on("click",function () {
            scan();
        });
        function scan() {
            wx.scanQRCode({
                needResult : 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType : [ "qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success : function(res) {

                    var res = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                    var pattern=/,/;
                    if(pattern.test(res)){
                        var s=res.split(",");
                        res=s[1];
                    }
                    $.post("<{link app=b2c ctl=wap_gallery act=getqrcode_data arg0='"+res+"'}>",function (re) {
                        $.ajax({
                            type: "GET",
                            cache: false,
                            url: re,
                            data: "",
                            success: function() {
                                location.href=re;
                            },
                            error: function() {
                                alert("此条码无效");
                            }
                        });
                    })
                }
            });
        }


    });




    (function(){
        $(".tobuy").unbind("click").bind("click",function(){
            addCart();

        });
        //add by hzk 2017-5-24  增加提示时间
        function popuphzk(msg) {
            alert(msg );
        }
        //add end
        //== 赠品封顶值提示方法 add by hzk 2017-5-18
//== 赠品封顶值提示方法 add by hzk 2017-5-18
        function checkmaxbuy(e1, value,inputtype) {

            //add by hzk 2017-5-18
            var checkgift = e1.find('.havegift').val();
            if('havegift'==checkgift){
                var getmaxbuy = e1.find('.maxbuy').val();

                if(inputtype=='button' && getmaxbuy == value){
                    //alert(2222222);
                    popuphzk('该商品购买数量已达赠品优惠上限'+getmaxbuy+'，上限之后的购买数量将不赠送赠品，确定继续购买？');
                }else if(inputtype=='text' && getmaxbuy<=value){
                    //alert(3333333);
                    popuphzk('该商品仅在前'+getmaxbuy+'个商品赠送赠品，上限数量之后的购买数量将不赠送赠品，确定继续购买？');
                }
                //alert(getmaxbuy);
            }
        }

        function setQuantity(el,flag){
            var getPeradd = parseInt(el.parent().find('.peradd').val());
            var ipt = el.parent().find('.num-ipt'),
                max1 = $('.maxbuy').val(),
                max  = $('#max').val(),
                nostoreSell = $('.nostoreSell').val();

            // alert(nostoreSell);
            if(nostoreSell==1){
                max  = 999999;
            }
            // max = parseInt(el.parents('.buy-form').find('.max-num').val()),
            min = parseInt(el.parents('.buy-form').find('.min-num').val()),
                val = parseInt(ipt.val());
            var res =parseInt(0);

            peradd = parseInt(("<{$data.add.isused}>" ==1) ?  "<{$data.add.pernum}>"  : parseInt(getPeradd)) ;
            //add by hzk
            //alert(peradd.val);
            min=peradd;

            $('.num-ipt').val(val);

            //add by hzk 2017-5-25
            if(flag==2){
                checknum=val+peradd;
                //alert(el.parent());
                checkmaxbuy(el.parent(), checknum,'button');
            }
            if(flag==3){
                //alert(el.parent());
                checkmaxbuy(el.parent(), val,'text');
            }
            //add end

            switch(flag){
                case 1:
                    if(!/^\d+$/.test(val))
                        ipt.val(min);
                    else if(val > min)
                    {	//hzk
                        if(!(0 ==(val%peradd))){
                            res = val - (val%peradd);
                        }else{
                            res = val - peradd;
                        }

                        ipt.val( res );
                        $('.num-ipt').val( res );
                    }
                    else if(val <= min)
                        popup('此商品最少购买' + min + '件');
                    break;
                case 2:
                    if(val < max)
                    {	//hzk
                        if(!(0 ==(val%peradd))){
                            res = (val- parseInt(val%peradd)) +  parseInt(peradd);
                        }else{
                            res = parseInt(val) + parseInt(peradd);
                        }
                        ipt.val( +parseInt(res) );
                        $('.num-ipt').val(+parseInt(res));
                    }
                    else if(val >= max)
                    {
                        ipt.val( max );
                        popup('此商品最多只能购买' + max + '件');
                    }
                    break;
                case 3:
                    if(!/^\d+$/.test(val)) {
                        ipt.val(min);
                    } else if(val > max){
                        popup('此商品最多只能购买' + max + '件');
                        ipt.val(max);
                    } else if(val < min){
                        popup('此商品最少购买' + min + '件');
                        ipt.val(min);
                    }
                    break;
            }
        }

        function popup(msg) {
            var tpl = $('<div class="popup">' + msg + '</div>').appendTo(document.body);
            var timer = setTimeout(function(){
                tpl.remove();
            }, 3000);
        }

        $('.minus').unbind("click").bind('click',function(e){
            var el = $(e.srcElement),
                n = 0;
            n=1;
            setQuantity(el,n);
        });

        $('.plus').unbind("click").bind('click',function(e){
            var el = $(e.srcElement),
                n = 0;
            n=2;
            setQuantity(el,n);
        });

        $('.num-ipt').bind('change',function(){
            setQuantity( $(this),3 );
        });

    })();

    $().ready(function() {
        $(".js-cancel").click(function(){
            $("body").attr("style","overflow:visible")
            $(".bg_inner").hide();
            $(".buy-form").hide();
        })
        $(".bg_inner").click(function(){
            $("body").attr("style","overflow:visible")
            $(".bg_inner").hide();
            $(".buy-form").hide();
        })
    });


    function tan(gid,pid,max,imgsrc,minprice,pernum,peradd,havegift,maxbuy,nostoreSell,name,standard){
        $("body").attr("style","overflow:hidden");
        $(".bg_inner").show();
        $(".buy-form").show();
        $('#goods_id').val(gid);
        $('#product_id').val(pid);
        $('#max').val(max);
        $('.min_img').attr('src',imgsrc);
        $(".min_price").html(minprice);
        $(".form_name").html(name);
        $(".form_standard").html(standard);
        $(".num-ipt").val(pernum);  //peradd 等参数 用样式或者input的id绑定；并且上面有<a onclick="tan( 		~~)
        $(".peradd").val(peradd);

        $('.havegift').val(havegift);
        $('.maxbuy').val(maxbuy);

        //alert(nostoreSell)
        $(".nostoreSell").val(nostoreSell);

        //$('#goods_num').max(maxbuy);

    }

    //    加入购物车动画
    function add_frame() {
        var $this=$(".buy-form").find(".min_img");
        var old_top=$this.offset().top;
        var old_left=$this.offset().left;
        var _width=$this.width();
        var _height=$this.height();
        var $new_img=$this.clone();
        var scroll=document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;

        if(old_top>scroll){
            old_top-=scroll;
            console.log("高度2为"+old_top);
        }
        $new_img.css({
            "position":"fixed",
            "width":"2.5rem",
            "height":"2.5rem",
            "left": old_left,
            "top": old_top,
            "z-index":"2222",
            "border-radius":"2rem"
        });
        $("body").append($new_img);

        $new_img.animate({
            "width":".5rem",
            "height":".5rem",
            "top":"11.8rem",
            "left":"4.2rem"
        },500,"swing");

        setTimeout(function () {
            $new_img.remove();
        },505);

    }

    function addCart(){
        var data = $('.buy-form').serialize();
        $.post($('.buy-form').attr('action'),data,function(re){
            //@mo 2017年5月8日 16:44:17
            if(re == 1)
            {
                popup("购买数量超过限制",2000);
                skuDialog.close();
            }
            else
            {
                if(re){
                    try{
                        result = $.parseJSON(re);
                        //alert(result);
                        popup(result.error,1000,"sm-pop");
                    } catch (e){

                        popup("已加入购物车",1000);//修改购物车为订货单 by wpn
                        add_frame();

//                        skuDialog.close();
                    }

                }
            }
        })

        setTimeout(function () {
            $(".bg_inner").hide();
            $(".buy-form").hide();
            $("body").attr("style","overflow:visible");
            if(isWeiXin()){
                window.location.href=window.location.href+"?id="+10000*Math.random();
            }
            else{
                location.reload();
            }
        },1000)

    }

    function isWeiXin(){
        var ua = window.navigator.userAgent.toLowerCase();
        if(ua.match(/MicroMessenger/i) == 'micromessenger'){
            return true;
        }else{
            return false;
        }
    }
</script>


</body>
</html>